/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vng.zing.zprofilefrontend.model;

import com.vng.zing.zprofilefrontend.common.Util;
import com.vng.zing.calc.thrift.Profile;
import com.vng.zing.calc.thrift.ProfileSession;
import com.vng.zing.logger.ZLogger;
import com.vng.zing.stats.Profiler;
import com.vng.zing.stats.ThreadProfiler;
import com.vng.zing.zcommon.thrift.PutPolicy;
import com.vng.zing.zprofilefrontend.common.Encription;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;

/**
 *
 * @author namhh
 */
public class AjaxChangePasswordModel extends BaseModel {

    private static final Logger _Logger = ZLogger.getLogger(LogInModel.class);
    public static final AjaxChangePasswordModel Instance = new AjaxChangePasswordModel();

    private AjaxChangePasswordModel() {
    }

    @Override
    public void process(HttpServletRequest req, HttpServletResponse resp) {
        ThreadProfiler profiler = Profiler.getThreadProfiler();
        resp.setContentType("text/html; charset=UTF-8");
        PrintWriter out = null;
        try {
            out = resp.getWriter();
            profiler.push(this.getClass(), "output");
            ///TODO..    
            if (null == req.getParameter("currentPassword") || null == req.getParameter("newPassword")) {
                out.println(Util.toJSON("ERROR", getProperties().get("en.INVALID_PASSWORD")));
            } else {
                ProfileSession profileSession = checkLogIn(req);
                if (null != profileSession) {
                    String currentPassword = req.getParameter("currentPassword");
                    String newPassword = req.getParameter("newPassword");
                    Profile profile = getClientWithCache().getCache(profileSession.getId());
                    String salt = String.format("%s%s", profile.getUsername(), profile.role.name());
                    String currentPasswordHash = Encription.getHash(currentPassword, salt);
                    if (currentPasswordHash.equals(profile.getPassword())) {
                        profile = getClient().changePassword(profileSession.getId(), currentPassword, newPassword);
                        if (null != profile) {
                            getClientWithCache().putCache(profile.getId(), profile, PutPolicy.ADD_OR_UDP);
                            out.println(Util.toJSON("SUCCESS", getProperties().get("en.changePassword.suceeded")));
                        } else {
                            out.println(Util.toJSON("ERROR", getProperties().get("en.changePassword.failed")));
                        }
                    } else {
                        out.println(Util.toJSON("ERROR", getProperties().get("en.changePassword.incorrectCurrent")));
                    }
                } else {
                    out.println(Util.toJSON("ERROR", getProperties().get("en.changePassword.notLogIn")));
                }
            }
            profiler.pop(this.getClass(), "output");
        } catch (Exception ex) {
            _Logger.error(null, ex);
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }
}
